package com.example.application22;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintSet;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView setting_tv_setting;
    private EditText setting_ed_ip;
    private EditText setting_ed_port;
    private EditText setting_ed_username;
    private EditText setting_ed_pwd;
    private EditText setting_ed_project;
    private EditText setting_ed_wangguan;
    private EditText setting_ed_temp;
    private EditText setting_ed_hum;
    private EditText setting_ed_light;
    private EditText setting_ed_body;
    private EditText setting_ed_greend;
    private EditText setting_ed_fan;
    private LinearLayout setting_ll_test;
    private TextView setting_but_save;
    private TextView setting_tv_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        setting_tv_setting = findViewById(R.id.setting_tv_setting);
        setting_ed_ip = findViewById(R.id.setting_ed_ip);
        setting_ed_port = findViewById(R.id.setting_ed_port);
        setting_ed_username = findViewById(R.id.setting_ed_username);
        setting_ed_pwd = findViewById(R.id.setting_ed_pwd);
        setting_ed_project = findViewById(R.id.setting_ed_project);
        setting_ed_wangguan = findViewById(R.id.setting_ed_wangguan);
        setting_ed_temp = findViewById(R.id.setting_ed_temp);
        setting_ed_hum = findViewById(R.id.setting_ed_hum);
        setting_ed_light = findViewById(R.id.setting_ed_light);
        setting_ed_body = findViewById(R.id.setting_ed_body);
        setting_ed_greend = findViewById(R.id.setting_ed_greend);
        setting_ed_fan = findViewById(R.id.setting_ed_fan);
        setting_but_save = findViewById(R.id.setting_tv_save);
        setting_tv_close = findViewById(R.id.setting_tv_close);

        setting_tv_setting.setOnClickListener(this);
        setting_but_save.setOnClickListener(this);
        setting_tv_close.setOnClickListener(this);

        SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);

        String setting_ed_ip_str = config.getString(Config.setting_ed_ip, "");
        String setting_ed_port_str = config.getString(Config.setting_ed_port, "");
        String setting_ed_username_str = config.getString(Config.setting_ed_username, "");
        String setting_ed_pwd_str = config.getString(Config.setting_ed_pwd, "");
        String setting_ed_project_str = config.getString(Config.setting_ed_project, "");
        String setting_ed_wangguan_str = config.getString(Config.setting_ed_wangguan, "");
        String setting_ed_temp_str = config.getString(Config.setting_ed_temp, "");
        String setting_ed_hum_str = config.getString(Config.setting_ed_hum, "");
        String setting_ed_light_str = config.getString(Config.setting_ed_light, "");
        String setting_ed_body_str = config.getString(Config.setting_ed_body, "");
        String setting_ed_greend_str = config.getString(Config.setting_ed_greend, "");
        String setting_ed_fan_str = config.getString(Config.setting_ed_fan, "");

        setting_ed_ip.setText(setting_ed_ip_str);
        setting_ed_port.setText(setting_ed_port_str);
        setting_ed_username.setText(setting_ed_username_str);
        setting_ed_pwd.setText(setting_ed_pwd_str);
        setting_ed_project.setText(setting_ed_project_str);
        setting_ed_wangguan.setText(setting_ed_wangguan_str);
        setting_ed_temp.setText(setting_ed_temp_str);
        setting_ed_hum.setText(setting_ed_hum_str);
        setting_ed_light.setText(setting_ed_light_str);
        setting_ed_body.setText(setting_ed_body_str);
        setting_ed_greend.setText(setting_ed_greend_str);
        setting_ed_fan.setText(setting_ed_fan_str);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.setting_tv_setting:
                finish();
                break;
            case R.id.setting_tv_save:
                SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                SharedPreferences.Editor edit = config.edit();

                String ip = setting_ed_ip.getText().toString();
                String port = setting_ed_port.getText().toString();
                String username = setting_ed_username.getText().toString();
                String pwd = setting_ed_pwd.getText().toString();
                String project = setting_ed_project.getText().toString();
                String wangguan = setting_ed_wangguan.getText().toString();
                String temp = setting_ed_temp.getText().toString();
                String hum = setting_ed_hum.getText().toString();
                String light = setting_ed_light.getText().toString();
                String body = setting_ed_body.getText().toString();
                String greend = setting_ed_greend.getText().toString();
                String fan = setting_ed_fan.getText().toString();

                edit.putString("setting_ed_ip",ip);
                edit.putString("setting_ed_port",port);
                edit.putString("setting_ed_username",username);
                edit.putString("setting_ed_pwd",pwd);
                edit.putString("setting_ed_project",project);
                edit.putString("setting_ed_wangguan",wangguan);
                edit.putString("setting_ed_temp",temp);
                edit.putString("setting_ed_hum",hum);
                edit.putString("setting_ed_light",light);
                edit.putString("setting_ed_body",body);
                edit.putString("setting_ed_greend",greend);
                edit.putString("setting_ed_fan",fan);

                edit.commit();
                break;
            case R.id.setting_tv_close:
                finish();
                break;
        }
    }
}